package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import domains.Teacher;

import mappers.TeacherMapper;
	
public class ShowTeachersGUI {
	private JFrame showTeachersFrame;
	private JButton addTeacherBtn, closeBtn;
	private JTable table;
	private JScrollPane scrollPane;
	
	public ShowTeachersGUI() {
		showTeachersFrame = new JFrame();
		showTeachersFrame.setTitle("Show Teachers");
		showTeachersFrame.setLayout(new BorderLayout());
		showTeachersFrame.setSize(600, 400);
			
		TeacherMapper tMapper = new TeacherMapper();
		List<Teacher> data = tMapper.selectAllTeachers();
		final MyTableModel m = new MyTableModel(data);
	
		table = new JTable(m);
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		
		DefaultTableCellRenderer leftRenderer = new DefaultTableCellRenderer();
		leftRenderer.setHorizontalAlignment(JLabel.LEFT);
		table.getColumnModel().getColumn(1).setCellRenderer(leftRenderer);
		
		addTeacherBtn = new JButton("Add Teacher(s)");
		addTeacherBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				AddTeacherGUI addTeacherGUI = new AddTeacherGUI();
			}
		});
		
		closeBtn = new JButton("Close");
		closeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				showTeachersFrame.setVisible(false);
			}
		});
		
		JPanel p = new JPanel(new FlowLayout());
		p.add(addTeacherBtn);
		p.add(closeBtn);
		
		showTeachersFrame.add(scrollPane, BorderLayout.CENTER);
		showTeachersFrame.add(p, BorderLayout.SOUTH);
		showTeachersFrame.pack();
		showTeachersFrame.setVisible(true);
		
	}
}

	class MyTableModel extends AbstractTableModel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		private List<Teacher> teachers = new ArrayList<Teacher>();
		
		
		public MyTableModel(List<Teacher> teachers){
			this.teachers = teachers;		
		}
		
		public void addTeacher(Teacher t){
			teachers.add(t);
			int row = teachers.size() - 1;
			fireTableRowsInserted(row, row);
		}
		
		public List<Teacher> getTeachers() {
			return new ArrayList<Teacher>(teachers);
		}
		
		@Override
		public int getColumnCount() {
			return 3;
		}

		@Override
		public int getRowCount() {
			return teachers.size();
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			Teacher t = teachers.get(rowIndex);
			
			switch(columnIndex) {
			case 0: 
				return t.getTeacherID();
				
			case 1: 
				return t.getTeacherName();
				
			case 2: 
				return t.getSubjectName();
			
			default:
				return "";
			}
		}
		
		public String getColumnName(int column){
			switch(column) {
			case 0: 
				return "Teacher ID";
				
			case 1: 
				return "Teacher Name";
				
			case 2: 
				return "Subject name";
			
				default:
					return "";
			}
		}
		
		public Class getColumnClass(int columnIndex){
			switch(columnIndex){
			case 1: 
				return Integer.class;
			
			case 2: 
				return String.class;
				
			case 3: 
				return String.class;
				
			default:
				return String.class;			
			}	
		}
	}
	
